window.onload=function(){
    var config = {
        elem:"#system",
        url:"/hot-word/sys-index",
        cols:[[
            {field:'id', width:105, title: '编号'},
            {field:'word', minWidth:105, title: '热词'},
            {field:'type', minWidth:105, title: '来源',templet:function(i){
                    switch (parseInt(i.type)){
                        case 1: return 'B端用户';
                        default: return 'C端用户';
                    }
                }},
            {field:'search_times', minWidth:105, title: '搜索次数'},
            {field:'create_time', minWidth:150, title: '添加时间',templet:function(i){
                    return timestampToTime(parseInt(i.create_time));
                }},
            {field:'update_time', minWidth:150, title: '最后修改时间',templet:function(i){
                    return timestampToTime(parseInt(i.update_time));
                }},
            { width:105, title: '操作',toolbar:"#operation"},
        ]],
        page:true
    };

    layui.use(['jquery','table','layer','form'],function(){
        var $ = layui.jquery, table = layui.table;
        var form = layui.form;

        table.render(config);
        table.on('tool(system)',function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent==='delate'){
                Utils.layer.confirm('是否删除该热词？',function(){
                    $.ajax({
                        url:'/hot-word/del-hot-word',
                        type:'post',
                        data:{id:data.id},
                        dataType:'json',
                        success:function(data){
                            Utils.layer.msg(data.msg);
                            table.render(config);
                        }
                    })
                })
            }
        });

        //添加热词
        $("#add-hotword").on('click',function () {
            Utils.layer.open({
                "content":[
                    '<form class="layui-form" action="">',
                        '<div class="layui-form-item">',
                            '<label class="layui-form-label">热词</label>',
                            '<div class="layui-input-block">',
                                '<input type="text" id="service_rate" name=""  placeholder="请输入热词" autocomplete="off" class="layui-input" >',
                             '</div>' ,
                        '</div>' ,
                        '<div class="layui-form-item">',
                            '<label  class="layui-form-label">展示位置</label>',
                            '<div class="item">',
                                 '<input type="radio" title="C端系统" value="0" name="type" checked/>',
                                 '<input type="radio" title="B端系统" value="1" name="type"/>',
                             '</div> ',
                         '</div> ',
                    '</form>'
                ].join(''),
                "btn":['确认','取消'],
                "yes":function () {
                    var words = $("#service_rate").val();
                    var type = $(":radio[name='type']:checked").val();

                    if(words == ' ' || words == null){
                        Utils.layer.msg('请输入热词');
                        return false;
                    }
                    $.ajax({
                        url:'/hot-word/add-hot-word',
                        type:'post',
                        data:{
                            word:words,
                            type:type,
                        },
                        dataType:'json',
                        success:function(data){
                            Utils.layer.msg(data.msg);
                            table.render(config);

                        }
                    })

                },
                "btn2":function () {
                    Utils.layer.closeAll();
                },
            });
            form.render();

        });
    })
};